package org.iteye.bbjava.util.abstractfactory.concre;

import org.iteye.bbjava.util.abstractfactory.AbstractFactory;
import org.iteye.bbjava.util.abstractfactory.ImplementClassProduct;
import org.iteye.bbjava.util.abstractfactory.InterfaceProduct;
import org.iteye.bbjava.util.interpreter.Context;
import org.iteye.bbjava.util.interpreter.NonTerminalExpression;
import org.iteye.bbjava.util.interpreter.TerminalExpression;

/**
 * This Class is one of java file code Factory, creates controller java files;
 * @author Thinkpad
 *
 */
public class JavaControllerFileCodeFactory implements AbstractFactory {
	String entityXml;
	
	

	public JavaControllerFileCodeFactory() {
		super();
	}
	
	

	public JavaControllerFileCodeFactory(String entityXml) {
		super();
		this.entityXml = entityXml;
	}



	@Override
	public InterfaceProduct createInterface() {
		Context ctx = new Context();
		InterfaceProduct interfaceProduct = null;
		if (null != this.entityXml && !"".equals(entityXml)) {
			interfaceProduct = new InterfaceProudctImpl();
			ctx.setXml(entityXml);
			NonTerminalExpression nte = new NonTerminalExpression();
			TerminalExpression te = new TerminalExpression();
			nte.setContext(ctx);
			te.setContext(ctx);
			String[] interfaceName = nte.interpret();
			String propertiesAndMethods = nte
					.createFinalResult("createInterface");
			String codeStr = this.createJavaControllerFileCode(
					interfaceName[0], propertiesAndMethods, "");
			interfaceProduct.setCodeStr(codeStr);
		}
		return interfaceProduct;

	}

	@Override
	public ImplementClassProduct createImplementClass() {
		Context ctx = new Context();
		ImplementClassProduct implementClassProduct = null;
		if(null !=this.entityXml && !"".equals(entityXml)){
			implementClassProduct = new  ImplementClassProductImpl();
			ctx.setXml(entityXml);
			NonTerminalExpression nte = new NonTerminalExpression();
			TerminalExpression te = new TerminalExpression();			
			nte.setContext(ctx);
			te.setContext(ctx);
			String[] classinterfaceName = nte.interpret();
			String propertiesAndMethods = nte.createFinalResult("createImplementClass");
			String codeStr = this.createJavaControllerFileCode(classinterfaceName[0], propertiesAndMethods, "");
			implementClassProduct.setCodeStr(codeStr);
		}
		return implementClassProduct;
	}

	public String createJavaControllerFileCode(String className,
			String propertiesAndMethods, String packageName) {
		StringBuffer sb = new StringBuffer();
		if(null!= className && !"".equals(className) && null!= propertiesAndMethods && !"".equals(propertiesAndMethods)){
			
			if(null != packageName ||!"".equals(packageName)){
				sb.append("package "+ packageName +";");
			}
			sb.append("public class "+className+"Controller extends BaseAction { ");
			sb.append(propertiesAndMethods);
			sb.append(" } ");
		}		
		return sb.toString();

	}

	@Override
	public void setEntityXml(String xml) {
		this.entityXml = xml;
		
	}

}
